package com.dfbz.demo01_卖票案例;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
public class Demo13_Callable实现线程 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {


        MyCallable task = new MyCallable();

        FutureTask<String> futureTask1 = new FutureTask<>(task);
        FutureTask<String> futureTask2 = new FutureTask<>(task);


        // 开启线程执行这个任务
        new Thread(futureTask1).start();
        new Thread(futureTask2).start();


        // 获取Callable的返回值
        String res_1 = futureTask1.get();
        String res_2 = futureTask1.get();


        System.out.println(res_1);
        System.out.println(res_2);
    }
}

class MyCallable implements Callable<String> {

    @Override
    public String call() throws Exception {
        for (int i = 0; i < 100000; i++) {
            System.out.println(Thread.currentThread().getName() + "【" + i + "】");
        }

        return "ok";
    }
}